//
//  SendRequest.m
//  SaintiFrameWork
//
//  Created by yan.jm on 14-5-6.
//  Copyright (c) 2014年 yan.jm. All rights reserved.
//

#import "SendRequest.h"
#import "SaintiNetWork.h"

@implementation SendRequest
+ (NSURLSessionDataTask *)orderDetailWithURL:(NSString *)url result:(void (^)(NSDictionary *result, NSError *error))block{
    
  return [[SaintiNetWork sharedClient] GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
       
   } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
//       NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
//       
//       NSData *JSONData = [str dataUsingEncoding:NSUTF8StringEncoding];
       
       NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
       
       if (block) {
           block(responseJSON,nil);
       }
       
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
       
       NSLog(@"error::::::~~~~~~~~~~~~~~~~%@",error);
       
       if (block) {
           block(nil,error);
       }

       
   }];



}
/*
 方法说明:
 AFNetWorking的POST请求实例
 
 参数说明:
 userName:请求参数
 passWord:
 返回结果:
 NSDictionary *result；block的返回结果，这个可以自定义，可以是数组，字典，也可以是一个实例类，建议实例类，这样用起来比较方便，代码易读。
 */
+ (NSURLSessionDataTask *)loginWithNSMutableDictionary:(NSDictionary *)dataDic   withNsstring:(NSString *)action result:(void (^)(NSDictionary *result, NSError *error))block{
    //123456
  return  [[SaintiNetWork sharedClient] POST: action parameters:dataDic progress:^(NSProgress * _Nonnull uploadProgress) {
      
      
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
//        NSLog(@"+++++++++++%@",responseJSON);
        
        if (block) {
            block(responseJSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"获取失败原因===%@",error.localizedDescription);
        
        if (block) {
            block(nil,error);
        }
    }];
    
    
}
+ (NSURLSessionDataTask *)UpLoginImgData:(NSData *)imgData withImgKey:(NSString *)imgKey WithNSMutableDictionary:(NSDictionary *)dataDic   withNsstring:(NSString *)action  result:(void (^)(NSDictionary *result, NSError *error))block{

    [SaintiNetWork sharedClient].responseSerializer = [AFCompoundResponseSerializer serializer];
    
    return  [[SaintiNetWork sharedClient] POST:action parameters:dataDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    [formData appendPartWithFileData:imgData name:imgKey fileName:[NSString stringWithFormat:@"personImg.png"] mimeType:@"image/png,image/jpeg,image/jpg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (block) {
            block(responseJSON,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (block) {
            block(nil,error);
        }

    }];
}


+ (NSURLSessionDataTask *)ImgsFromToArr:(NSArray  *)imgArr withImgKey:(NSString *)imgKey WithNSMutableDictionary:(NSDictionary *)dataDic   withNsstring:(NSString *)action  result:(void (^)(NSDictionary *result, NSError *error))block{
    
[SaintiNetWork sharedClient].responseSerializer = [AFCompoundResponseSerializer serializer];

return  [[SaintiNetWork sharedClient] POST:action parameters:dataDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
    for (NSInteger imgCout = 0; imgCout<imgArr.count; imgCout++) {
        NSData * imgData = UIImageJPEGRepresentation(imgArr[imgCout], 0.4);
        
        [formData appendPartWithFileData:imgData name: [NSString stringWithFormat:@"%@%ld",imgKey,imgCout] fileName:[NSString stringWithFormat:@"jiayiPing%ld.png",imgCout] mimeType:@"image/png,image/jpeg,image/jpg"];
        
    }} progress:^(NSProgress * _Nonnull uploadProgress) {
    
    
} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    if (block) {
        block(responseJSON,nil);
    }
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    if (block) {
        block(nil,error);
    }
    
}];
//
}
//
@end
